<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Marketing;


use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Good;
use App\Models\JinGangAnew;
use App\Models\Order;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class JinController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\Marketing\JinController";

    public function index(Request $request)
    {
        $pageSize = $request->pageSize;
        $sql = JinGangAnew::select('*');
        $jinList = $sql->orderBy('isIndex', 'desc')
            ->orderBy('indexTime', 'desc')
            ->orderByDesc('createtime')
            ->paginate($pageSize)
            ->toArray();
        $data['jinList'] = $jinList['data'];
        foreach ($data['jinList'] as $key => $value) {
            $data['jinList'][$key]['image'] = path($value['image']);
            $data['jinList'][$key]['beijing_image'] = path($value['beijing_image']);
            if (isset($value['createtime'])) {
                $data['jinList'][$key]['createtime'] = date("Y-m-d H:i:s", $value['createtime']);
            }
        }
        $data['totalNum'] = $jinList['total'];
        return $this->apiResponse($data);
    }

    public function show(Request $request, $id)
    {
        $jin = JinGangAnew::select('*')
            ->from((new JinGangAnew())->getTable() . ' as t1')
            ->where('t1.id', $id)
            ->first();
        $jin['image'] = path($jin['image']);
        return $this->apiResponse($jin);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $data = $request->only('name', 'image', 'beijing_image', 'status', 'color', 'color1');
        if (!isset($data['name']) || !isset($data['image']) || !isset($data['beijing_image']) || !isset($data['status']) || !isset($data['color']) || !isset($data['color1'])) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $num = JinGangAnew::where('id', '!=', $id)->where('name', $data['name'])->count();
        if ($num) {
            return $this->apiResponse('', config('errorLiu.GAIJINGANGQUYMINGCHENYICUNZAZI'));
        }
        if ($id) {
            if($data['status'] == 2){
                $num = Banner::where('jingang_id', $id)->count();
                if ($num) {
                    return $this->apiResponse('', config('errorLiu.GAINEIRONGBEISHEZHJWEISHOUYE'));
                }
            }
            $content = "修改";
            JinGangAnew::where('id', $id)->update($data);
        } else {
            $content = "新增";
            $data['createtime'] = time();
            $id = JinGangAnew::insertGetId($data);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = $content;
        $content .= "了id为【".$id."】的金刚区,内容为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "金刚区模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function update(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        JinGangAnew::where('id', $id)->update([
            'isIndex' => 1,
            'indexTime' => date("Y-m-d H:i:s")
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/update";
        $title = "修改";
        $content = "将id为【".$id."】的金刚区分类置顶";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "金刚区模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
//        $jin = JinGangAnew::where('id', $id)->first();
//        if ($jin['isIndex']) {
//            JinGangAnew::where('id', $id)->update([
//                'isIndex' => 0,
//                'indexTime' => null
//            ]);
//        } else {
//            JinGangAnew::where('id', $id)->update([
//                'isIndex' => 1,
//                'indexTime' => date("Y-m-d H:i:s")
//            ]);
//        }
        return $this->apiResponse(true);
    }

    public function destroy(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $num = Banner::where('jingang_id', $id)->count();
        if ($num) {
            return $this->apiResponse('', config('errorLiu.GAINEIRONGBEISHEZHJWEISHOUYE'));
        }
        $jingang = JinGangAnew::select()->where('id', $id)
            ->first();
        JinGangAnew::where('id', $id)->delete();
        Good::where('jingang_id', $id)->update([
            'jingang_id' => null,
            'jinIndex' => 0,
            'jinIndexTime' => date("Y-m-d H:i:s")
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了id为【".$id."】的金刚区,内容为【".json_encode($jingang)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "金刚区模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function goodList(Request $request)
    {
        $id = $request->id;
        $name = $request->name;
        $sql = Good::select('id', 'name')
            ->where('is_del', 0)
            ->where('status', 1)
            ->where('jingang_id', null);
        if (isset($id)) {
            $sql->where('id', $id);
        }
        if (isset($name)) {
            $sql->where('name', 'like', "%$name%");
        }
        $goodList = $sql->orderBy('createtime', 'desc')
            ->get()
            ->toArray();
        return $this->apiResponse($goodList);
    }
}